#pragma once

#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>

#define NON_BLOCK_ERR 5

void SetNonBlockOrDie(int fd)
{
    int cur_flag = fcntl(fd, F_GETFL);
    if (cur_flag < 0)
    {
        perror("fcntl - F_GETFL");
        exit(NON_BLOCK_ERR);
    }

    if (fcntl(fd, F_SETFL, cur_flag | O_NONBLOCK) < 0)
    {
        perror("fcntl - F_SETFL");
        exit(NON_BLOCK_ERR);
    }
    cout << "Set fd " << fd << " to non-blocking mode done" << endl;
}